package com.adinall.dsp.core.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.adinall.dsp.db.entity.RPTOrderHourEntity;

/**
 * 集合工具类
 * 
 * @author 高万超
 * @version V1.0.0 $ 2017年5月24日 上午11:58:05
 */
public final class CollectionUtils {

    public static String join(Object[] obj, String regex) {

        if (CheckUtils.isEmpty(obj)) {
            return "";
        }

        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < obj.length; i++) {

            buf.append(obj[i]);

            if (i < obj.length - 1) {
                buf.append(regex);
            }
        }

        return StringUtils.toString(buf);
    }

    public static String join(Object[] obj) {
        return join(obj, ",");
    }

    public static <T> String join(Collection<T> data, String regex) {

        if (CheckUtils.isEmpty(data)) {
            return "";
        }

        StringBuffer buf = new StringBuffer();

        int idx = 0;

        for (T t : data) {

            buf.append(t);

            if (idx < data.size() - 1) {
                buf.append(regex);
            }

            idx++;
        }

        return StringUtils.toString(buf);
    }

    public static <T> String join(Collection<T> data) {
        return join(data, ",");
    }

    public static <K, V> String join(Map<K, V> map, String regex) {

        if (CheckUtils.isEmpty(map)) {
            return "";
        }

        StringBuffer buf = new StringBuffer();

        int idx = 0;

        for (Entry<K, V> entry : map.entrySet()) {

            Object obj = entry.getValue();
            if (obj instanceof Object[]) {

                Object[] arr = (Object[]) obj;

                for (int i = 0; i < arr.length; i++) {

                    buf.append(entry.getKey());
                    buf.append("=");
                    buf.append(arr[i]);

                    if (i < arr.length - 1) {
                        buf.append(regex);
                    }
                }
            } else {
                buf.append(entry.getKey());
                buf.append("=");
                buf.append(obj);
            }

            if (idx < map.size() - 1) {
                buf.append(regex);
            }

            idx++;
        }

        return StringUtils.toString(buf);
    }

    public static <T> Collection<T> copy(Collection<T> src) {

        Collection<T> dest = null;

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        ByteArrayInputStream is = null;
        ObjectInputStream in = null;
        try {

            out = new ObjectOutputStream(os);
            out.writeObject(src);
            is = new ByteArrayInputStream(os.toByteArray());
            in = new ObjectInputStream(is);
            dest = (Collection<T>) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.close(os);
            IOUtils.close(out);
            IOUtils.close(is);
            IOUtils.close(in);
        }
        return dest;
    }

    public static boolean isEmpty(Collection collection) {
        return (collection == null || collection.isEmpty());
    }

    /**
     * 方法不用了，功能转移到存储过程中
     */
    @Deprecated
    public static List<RPTOrderHourEntity> reportVariance(List<RPTOrderHourEntity> orderList, List<RPTOrderHourEntity> hourList,
            List<RPTOrderHourEntity> reportList, boolean isSummary) {
        double revenues = 0.0d;
        for (int j = 0; j < orderList.size(); j++) {

            RPTOrderHourEntity orderRpt = orderList.get(j);
            RPTOrderHourEntity hourRpt = hourList.get(j);

            String orderDate = orderRpt.getDate();
            revenues = orderRpt.getRevenues() - hourRpt.getRevenues();

            long shows = orderRpt.getShows() - hourRpt.getShows();
            long clicks = orderRpt.getClicks() - hourRpt.getClicks();

            for (int i = 0; i < reportList.size(); i++) {

                RPTOrderHourEntity baseReport = reportList.get(i);
                String baseDate = baseReport.getDate();

                if (isSummary) {
                    if (baseReport.getHour() == 2) {
                        baseReport.setShows(baseReport.getShows() + shows);
                        baseReport.setClicks(baseReport.getClicks() + clicks);
                        baseReport.setRevenues(baseReport.getRevenues() + revenues);
                        reportList.set(i, baseReport);
                    }
                } else {
                    if (baseReport.getHour() == 2 && baseDate.equals(orderDate)) {
                        baseReport.setShows(baseReport.getShows() + shows);
                        baseReport.setClicks(baseReport.getClicks() + clicks);
                        baseReport.setRevenues(baseReport.getRevenues() + revenues);
                        reportList.set(i, baseReport);
                    }
                }
            }
        }
        return reportList;
    }
}